const express = require('express');
const router = express.Router();

const SsqSchema = require('../../schemas/ssqSchema');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.get('/bluePercent', function (req, res, next) {
  let match = {};
  let day = req.query.day;
  if(day){
    match.day = {'$gte': day};
  }
  let sequence = Number(req.query.sequence);
  if(!Number.isNaN(sequence) && sequence>0){
    match.sequence = {'$gte': sequence};
  }
  let promise = SsqSchema.aggregate([
    {
      '$match': match
    },
    {
      '$group': {
        '_id': '$lottery.blue',
        'total': {'$sum': 1}
      }
    },{
      '$sort': {
        _id: 1
      }
    }
  ]);
  promise.then((result)=> {
    return res.json(result);
  }).catch((err)=> {
    return res.json('fail');
  });
});

module.exports = router;
